package com.czmini.cztest.controller;

import com.alibaba.fastjson.JSONObject;
import com.czmini.cztest.common.TokenDb;
import com.czmini.cztest.config.UserBaseStr;
import com.czmini.cztest.constants.UserConstants;
import com.czmini.cztest.dto.PageTableRequest;
import com.czmini.cztest.dto.PageTableResponse;
import com.czmini.cztest.dto.ResultDto;
import com.czmini.cztest.dto.TokenDto;
import com.czmini.cztest.dto.testcase.AddTestCaseDto;
import com.czmini.cztest.dto.testcase.QueryTestCaseListDto;
import com.czmini.cztest.dto.testcase.UpdateZcTestCaseDto;
import com.czmini.cztest.entity.ZcTestCase;
import com.czmini.cztest.service.ZcTestCaseService;
import com.czmini.cztest.util.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

/**
 * @ProjectName: cztest
 * @Package: com.czmini.cztest.controller
 * @ClassName: ZcTestCaseController
 * @Author: 还是那个橙子
 * @Description: 用例管理模块
 * @Date: 2022/3/25 17:11
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = " zctest-测试用例管理")
@Slf4j
public class ZcTestCaseController {
    @Autowired
    private ZcTestCaseService zcTestCaseService;

    @Autowired
    private TokenDb tokenDb;

    @ApiOperation(value = "批量新增测试用例", notes="仅用于测试用户")
    @PostMapping("text")
    public ResultDto saveTestcaseText(HttpServletRequest request, @RequestBody AddTestCaseDto addTestCaseDto){
        log.info("新增测试用例文本", JSONObject.toJSONString(addTestCaseDto));

        //非空判断
        if (Objects.isNull(addTestCaseDto)){
            return  ResultDto.fail("请参不能为空");
        }
        if (StringUtils.isEmpty(addTestCaseDto.getCaseData())){
            return  ResultDto.fail("测试数据不能为空");
        }
        if (StringUtils.isEmpty(addTestCaseDto.getCaseName())){
            return  ResultDto.fail("测试用例名称不能为空");
        }
        //实体类
        ZcTestCase zcTestCase = new ZcTestCase();
        // 从dto 赋值到实体类
        BeanUtils.copyProperties(addTestCaseDto,zcTestCase);
        //token验证
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));

        System.out.println(tokenDto);
        zcTestCase.setCreateUserId(tokenDto.getUserId());

        ResultDto resultDto = zcTestCaseService.save(zcTestCase);
        return  resultDto;
    }


    @ApiOperation(value = "文件上传", notes="仅用于测试用户")
    @PostMapping("file")
    public ResultDto saveTestcaseText(HttpServletRequest request,
                                      @RequestParam("caseFile") MultipartFile casefile , @RequestBody AddTestCaseDto addTestCaseDto) throws IOException {
        log.info("新增测试用例文本", JSONObject.toJSONString(addTestCaseDto));

        if (Objects.isNull(addTestCaseDto)){
            return  ResultDto.fail("请参不能为空");
        }
        if (Objects.isNull(casefile)){
            return  ResultDto.fail("测试数据不能为空");
        }
        if (StringUtils.isEmpty(addTestCaseDto.getCaseName())){
            return  ResultDto.fail("测试用例名称不能为空");
        }
        // 输入流
        InputStream inputStream = casefile.getInputStream();
        String caseData = IOUtils.toString(inputStream, "utf-8");
        inputStream.close();
        log.info("casedata",caseData);
        //token登录验证
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        ZcTestCase zcTestCase = new ZcTestCase();
        zcTestCase.setCreateUserId(tokenDto.getUserId());
        zcTestCase.setCaseData(caseData);
        ResultDto resultDto = zcTestCaseService.save(zcTestCase);
        return  resultDto;
    }



    @ApiOperation(value = "修改测试用例")
    @PutMapping
    public ResultDto<ZcTestCase> update(HttpServletRequest request, @RequestBody UpdateZcTestCaseDto updateZcTestCaseDto){
        log.info("新增测试用例文本", JSONObject.toJSONString(updateZcTestCaseDto));

        //非空判断
        if (Objects.isNull(updateZcTestCaseDto)){
            return  ResultDto.fail("请参不能为空");
        }
        if (StringUtils.isEmpty(updateZcTestCaseDto.getCaseData())){
            return  ResultDto.fail("测试数据不能为空");
        }
        if (StringUtils.isEmpty(updateZcTestCaseDto.getCaseName())){
            return  ResultDto.fail("测试用例名称不能为空");
        }
        //实体类
        ZcTestCase zcTestCase = new ZcTestCase();
        // 从dto 赋值到实体类
        BeanUtils.copyProperties(updateZcTestCaseDto,zcTestCase);
        //token验证
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        System.out.println(tokenDto);
        zcTestCase.setCreateUserId(tokenDto.getUserId());
        ResultDto<ZcTestCase> resultDto = zcTestCaseService.update(zcTestCase);
        return  resultDto;
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResultDto<PageTableResponse<ZcTestCase>> list(HttpServletRequest request,
                                                         PageTableRequest<QueryTestCaseListDto> pageTableRequest){
        log.info("测试用例列表查询-入参= "+ JSONObject.toJSONString(pageTableRequest));
        if (Objects.isNull(pageTableRequest)){
            return ResultDto.success("列表查询参数不能为空");
        }
        //token
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        QueryTestCaseListDto params = pageTableRequest.getParams();

        if (Objects.isNull(params)){
            params= new QueryTestCaseListDto();
        }

        params.setCreateUserId(tokenDto.getUserId());
        pageTableRequest.setParams(params);

        ResultDto<PageTableResponse<ZcTestCase>> pageTableResponseResultDto = zcTestCaseService.list(pageTableRequest);

        return  pageTableResponseResultDto;
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{caseId}")
    public ResultDto<ZcTestCase> getById(HttpServletRequest request,@PathVariable Integer caseId){
        log.info("根据id查询-入参= "+ caseId);
        if (Objects.isNull(caseId)){
            return ResultDto.success("caseId不能为空");
        }
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        ResultDto<ZcTestCase> resultDto = zcTestCaseService.getById(caseId,tokenDto.getUserId());
        return  resultDto;
    }

    @ApiOperation(value = "根据id删除")
    @GetMapping("data/{caseId}")
    public ResultDto<ZcTestCase> deletById(HttpServletRequest request,@PathVariable Integer caseId){
        log.info("根据id查询-入参= "+ caseId);
        if (Objects.isNull(caseId)){
            return ResultDto.success("caseId不能为空");
        }
        TokenDto tokenDto = tokenDb.getTokenDto(request.getHeader(UserBaseStr.LOGIN_TOKEN));
        ResultDto<ZcTestCase> resultDto = zcTestCaseService.deleteById(caseId,tokenDto.getUserId());
        return  resultDto;
    }






}
